package com.juntuo.controller;

import com.juntuo.entity.*;
import com.juntuo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping("/chick")
public class ChickController {

    @Autowired
    private XjChickService xjChickService;
    @Autowired
    private XjUserPointsRecordService xjUserPointsRecordService;
    @Autowired
    private XjUserPointsService xjUserPointsService;
    @Autowired
    private XjChickPointsRecordService xjChickPointsRecordService ;
    @Autowired
    private XjChickExchangeRecordService xjChickExchangeRecordService;

    @Autowired
    private XjChickOrderService xjChickOrderService;

    @Autowired
    private FzlUserAddressService fzlUserAddressService;


    @RequestMapping("/index")
    public String toIndex(Integer uid,Model model){
        Integer userId=uid;
        XjChick chick = new XjChick();
        XjUserPoints userPoints = new XjUserPoints();
        userPoints.setUserId(userId);
        chick.setUserId(userId);
        chick = xjChickService.selectOne(chick);
        if(chick==null || chick.getId()==null){
            chick = new XjChick();
            chick.setChickPoints(0);
            chick.setDailyLimitPoints(700);
            chick.setMature(0);
//            chick.setUserPoints(0);
            chick.setUserId(userId);
            xjChickService.insert(chick);
            chick = xjChickService.selectOne(chick);

            //没有chick的情况下查询是否存在userpoints，如果存在 取 不存在 新建

            userPoints = xjUserPointsService.selectOne(userPoints);
            if(userPoints==null){
                userPoints= new XjUserPoints();
                userPoints.setUserId(userId);
                userPoints.setCreateTime(new Date());
                userPoints.setPoints(0);
                xjUserPointsService.insert(userPoints);
            }
        }
        userPoints = xjUserPointsService.selectOne(userPoints);
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        int month = c.get(Calendar.MONTH) + 1;

        Date lastFeedDate=xjChickPointsRecordService.selectLastFeedTimeByUserId(userId);
        Long lastFeedTime=lastFeedDate==null?0L:lastFeedDate.getTime();

        model.addAttribute("month",month);
        model.addAttribute("chick",chick);
        model.addAttribute("userPoints",userPoints);
        model.addAttribute("lastFeedTime",lastFeedTime);
        return "/chick/index";
    }
    @RequestMapping("/change")
    public String toChange(Integer userId,Model model){
        XjChick chick = new XjChick();
        XjUserPoints userPoints = new XjUserPoints();
        userPoints.setUserId(userId);
        chick.setUserId(userId);
        chick = xjChickService.selectOne(chick);
        if(chick.getMature()<1){
            return "redirect:/chick/index?uid="+userId;
        }
        FzlUserAddress address = new FzlUserAddress();
        address.setUserId(userId);
        List<FzlUserAddress> addresses=fzlUserAddressService.select(address);
        model.addAttribute("chick",chick);
        model.addAttribute("addresses",addresses);
        return "/chick/change";
    }
    @RequestMapping("/feed/{id}")
    @ResponseBody
    public Map<String,Object> feed(@PathVariable int id){
        Map<String,Object> result = new HashMap<>();
        XjUserPoints userPoints = xjUserPointsService.selectByPrimaryKey(id);
        Integer points = userPoints.getPoints();
        if(points>=20){
            XjChick chick = new XjChick();
            chick.setUserId(userPoints.getUserId());
            chick=xjChickService.selectOne(chick);
            if(chick.getDailyLimitPoints()<20){
                result.put("code",-2);//当日限制
                return result;
            }
            userPoints.setPoints(points-20);
            xjUserPointsService.updateByPrimaryKey(userPoints);

            XjUserPointsRecord userPointsRecord = new XjUserPointsRecord();
            userPointsRecord.setCreateTime(new Date());
            userPointsRecord.setPoints(20);
            userPointsRecord.setLeftPoints(points-20);
            userPointsRecord.setUserId(userPoints.getUserId());
            userPointsRecord.setRemark("喂食小鸡-20");
            xjUserPointsRecordService.insert(userPointsRecord);

            Integer chickPoints = chick.getChickPoints();
            Integer mature = chick.getMature();
            Integer limit = chick.getDailyLimitPoints();
            if(chickPoints==9980){
                mature++;
                chickPoints=0;
                chick.setChickPoints(chickPoints);
                chick.setMature(mature);
            }else{
                chickPoints=chickPoints+20;
                chick.setChickPoints(chickPoints);
            }
            limit=limit-20;
            chick.setDailyLimitPoints(limit);
            xjChickService.updateByPrimaryKey(chick);

            XjChickPointsRecord chickPointsRecord = new XjChickPointsRecord();
            chickPointsRecord.setCreateTime(new Date());
            chickPointsRecord.setPoints(20);
            chickPointsRecord.setTotalPoints(chickPoints);
            chickPointsRecord.setUserId(userPoints.getUserId());
            chickPointsRecord.setRemark("小鸡喂食+20");
            xjChickPointsRecordService.insert(chickPointsRecord);

            result.put("code",1);
            result.put("chick",chick);
            result.put("userPoints",userPoints);
            result.put("lastFeedTime",new Date().getTime());
            return result;
        }else {
            result.put("code",-1);//积分不够
            return result;
        }

    }


    @RequestMapping("/order")
    @ResponseBody
    public int exchange( int userId,int count,Integer addressId){

        XjChick xjChick = new XjChick();
        xjChick.setUserId(userId);
        XjChick chick = xjChickService.selectOne(xjChick);
        Integer left =  chick.getMature()-count;
        if(left>=0){
            chick.setMature(left);
        }else{
            return 0;
        }
        xjChickService.updateByPrimaryKey(chick);

        XjChickOrder order = new XjChickOrder();
        order.setAddressId(addressId);
        order.setCount(count);
        order.setLeftCount(left);
        order.setUserId(userId);
        order.setCreateTime(new Date());
        xjChickOrderService.insert(order);
        return 1;
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public int deleteOne(@PathVariable int id){
        return 1;
    }

    @RequestMapping("/address/list")
    @ResponseBody
    public List<FzlUserAddress> getAddress(Integer userId){
        FzlUserAddress address = new FzlUserAddress();
        address.setUserId(userId);
        return fzlUserAddressService.select(address);
    }

}
